﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization;
using System.Text;

namespace suirui.ZhuMu.Common
{
    public static class ApiGuard
    {
        public static void EnsureNotNull(object param, string paramName)
        {
            if (param == null) throw new BadRequestException($"{paramName} can not be null.");
        }

        public static void EnsureNotEmpty<T>(IEnumerable<T> collection, string paramName)
        {
            if (collection == null || collection.Count() == 0)
                throw new BadRequestException($"{paramName} can not be null or empty.");
        }

        public static void EnsureExist(object value, string message = "Not found")
        {
            if (value == null) throw new BadRequestException(message);
        }

        public static void EnsureNotExist(object value, string message = "Already existed")
        {
            if (value != null) throw new BadRequestException(message);
        }
    }

    [Serializable]
    internal class BadRequestException : Exception
    {
        public BadRequestException()
        {
        }

        public BadRequestException(string message) : base(message)
        {
        }

        public BadRequestException(string message, Exception innerException) : base(message, innerException)
        {
        }

        protected BadRequestException(SerializationInfo info, StreamingContext context) : base(info, context)
        {
        }
    }
}
